package org.yang.plugins.cloud.handler;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.yang.common.core.exception.BaseException;
import org.yang.common.core.result.R;
import org.yang.common.i18n.constant.I18nMessageConstant;
import org.yang.common.i18n.enums.RI18nEnum;

import java.util.Objects;
import java.util.Set;

/**
 * 全局异常拦截器
 *
 * @author: lslands
 * @version: 1.0
 * @create: 2024/5/10
 * @description:
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @Autowired
    private HttpServletRequest request;


    /**
     * 自定义异常拦截
     * @param e 入参
     * @return R<T>
     * @author lslands
     * @date 2024/5/10 23:37
     */

    @ExceptionHandler(BaseException.class)
    public <T> R<T> exceptionHandler(BaseException e) {
        if(e.getLevel()==(BaseException.REPEAT)){
            log.error("异常类型:{};请求地址:{};方式:{};参数:{};异常等级:{},异常信息:{} 已存在","【字段重复异常】",
                    request.getRequestURI(),request.getMethod(),
                    request.getQueryString(),e.getLevel(),e.getMessage());
            return R.fail(RI18nEnum.CONFLICT_FAIL,e.getMessage());
        }
        if(e.getLevel()==BaseException.NULL){
            log.error("异常类型:{};请求地址:{};方式:{};参数:{};异常等级:{},异常信息:{}","【查询数据为空】",
                    request.getRequestURI(),request.getMethod(),
                    request.getQueryString(),e.getLevel(),e.getMessage());
            return R.fail(RI18nEnum.NOT_NULL);
        }
        log.error("异常类型:{};请求地址:{};方式:{};参数:{};异常等级:{},异常信息:{}","【全局自定义异常】BaseException",
                request.getRequestURI(),request.getMethod(),
                request.getQueryString(),e.getLevel(),e.getMessage());
        return R.fail(e.getMessage());
    }


    /**
     * 请求方式异常
     * @param e 入参
     * @return R<T>
     * @author lslands
     * @date 2024/5/10 23:37
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public <T> R<T> exceptionHandler(HttpRequestMethodNotSupportedException e) {
        log.error("异常类型:{};请求地址:{};方式:{};参数:{};异常信息:{}","【请求方式错误】HttpRequestMethodNotSupportedException",
                request.getRequestURI(),request.getMethod(),
                request.getQueryString(),e.getMessage());
        return R.fail(e.getMessage());
    }

    /**
     * validation注解异常
     * @param e 异常
     * @return R<?>
     */
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R<?> exceptionHandler(MethodArgumentNotValidException e) {
        String message = Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage();
        log.warn("异常类型:{};请求地址:{};方式:{};参数:{};异常原因:{}","【validation注解校验异常】",
                request.getRequestURI(),request.getMethod(),
                request.getQueryString(),message);
        return R.fail(message);
    }
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    @ExceptionHandler(ConstraintViolationException.class)
    public R<?> exceptionHandler(ConstraintViolationException e) {
        Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
        String message = null;
        for (ConstraintViolation<?> constraintViolation : constraintViolations) {
            log.warn("异常类型:{};请求地址:{};方式:{};参数:{};异常原因:{}","【validation注解校验异常】",
                    request.getRequestURI(),request.getMethod(),
                    request.getQueryString(),constraintViolation.getMessage());
            message= constraintViolation.getMessage();
        }
        return R.fail(message);
    }

    /**
     * 缺少请求参数
     * @param e MissingServletRequestParameterException
     * @return R<?>
     */
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public R<?> exceptionHandler(MissingServletRequestParameterException e) {
        log.warn("异常类型:{};请求地址:{};方式:{};参数:{}","【缺少请求参数】",
                request.getRequestURI(),request.getMethod(),
                request.getQueryString());
         return R.fail(I18nMessageConstant.VALIDATION_ID_EMPTY);
    }
}
